import './public-path';
import { start } from 'qiankun';
import { setSingleStore } from './utils/single-store';

export { Components, JSX } from './components';
export { getSingleStore } from './utils/single-store';
export { AppReuseStrategy } from './utils/router-guard/app-reuse-strategy';

import { getAscendingId, AppPropObject } from '@ds/ds-micro/dist';

console.log('单例依赖被执行', getAscendingId());
/** 初始乾坤微前端 */
export function initStart() {
  console.log('调用微前端第三方库初始化');
  start({
    singular: false,
  });
}

// 判断是否是微前端
export const isMicro = (window as any).__MICRO_APP_BASE_ROUTE__ || window.__POWERED_BY_QIANKUN__;

let lifeCycleCalls = {
  bootstrap: _data => {},
  mount: _data => {},
  unmount: (_data) => {},
};

// 加入生命周期回调
export function lifeCycleCallBack(lifeCycleCB: Object) {
  lifeCycleCalls = { ...lifeCycleCalls, ...lifeCycleCB };
}

export async function bootstrap(_props: AppPropObject) {
  if (_props.singleSpa) {
    _props.singleSpa.start();
  }
  if (_props.data) {
    setSingleStore(_props.data);
  }
  if (_props.systemData) {
    setSingleStore(_props.systemData, 'systemData');
  }
  lifeCycleCalls.bootstrap(_props);
  // console.log('粘合剂: bootstrap:', _props);
}

export async function mount(_props: AppPropObject) {
  if (_props.singleSpa) {
    _props.singleSpa.start();
  }

  if (_props.data) {
    setSingleStore(_props.data);
  }
  if (_props.systemData) {
    setSingleStore(_props.systemData, 'systemData');
  }
  lifeCycleCalls.mount(_props);
  console.log('粘合剂:加载', _props);
}

export async function unmount(_props: AppPropObject) {
  lifeCycleCalls.unmount(_props);
  // console.log('粘合剂: 卸载', props);
}
